package cn.iocoder.yudao.module.train.dal.mysql.student;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.train.dal.dataobject.WxUser.WxUserDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentUserDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.student.StudentDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.train.controller.admin.student.vo.*;

import java.util.List;

/**
 * 学生用户 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface StudentMapper extends BaseMapperX<StudentDO> {

//    default PageResult<StudentDO> selectPage(StudentPageReqVO reqVO) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<StudentDO>()
//                .likeIfPresent(StudentDO::getStudentName, reqVO.getStudentName())
//                .betweenIfPresent(StudentDO::getStudentBirthdate, reqVO.getStudentBirthdate())
//                .eqIfPresent(StudentDO::getStudentAge, reqVO.getStudentAge())
//                .eqIfPresent(StudentDO::getStudentPhonenumber, reqVO.getStudentPhonenumber())
//                .eqIfPresent(StudentDO::getStudentSex, reqVO.getStudentSex())
//                .eqIfPresent(StudentDO::getStudentStatus, reqVO.getStudentStatus())
//                .betweenIfPresent(StudentDO::getCreateTime, reqVO.getCreateTime())
//                .orderByDesc(StudentDO::getId));
//    }
    default PageResult<StudentUserDTO> selectPage(StudentPageReqVO reqVO) {
        // 创建MyBatis-Plus分页对象
        Page<StudentUserDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());

        // 创建连接查询条件
        MPJLambdaWrapper<StudentDO> wrapper = new MPJLambdaWrapper<StudentDO>()
                .selectAll(StudentDO.class)  // 选择ProisDO所有字段
                .select(WxUserDO::getUName)
                .selectAs(WxUserDO::getId, StudentUserDTO::getUserId)
                .select(WxUserDO::getUOpenId)
                .select(WxUserDO::getUAvatar)
                .select(WxUserDO::getUStatus)// 选择SchoolDO的name字段
                .leftJoin(WxUserDO.class, on -> on
                        .eq(WxUserDO::getId, StudentDO::getUserId)
                );  // 连接条件

        // 添加与原方法相同的过滤条件
        if (reqVO.getStudentName() != null) {
            wrapper.like(StudentDO::getStudentName, reqVO.getStudentName());
        }
        if (reqVO.getStudentPhoneNumber() != null) {
            wrapper.like(StudentDO::getStudentPhoneNumber, reqVO.getStudentPhoneNumber());
        }
        if (reqVO.getStudentAge() != null) {
            wrapper.eq(StudentDO::getStudentAge, reqVO.getStudentAge());
        }
        if (reqVO.getStudentSex() != null) {
            wrapper.eq(StudentDO::getStudentSex, reqVO.getStudentSex());
        }
        if (reqVO.getVipFlag() != null) {
            wrapper.eq(StudentDO::getVipFlag, reqVO.getVipFlag());
        }
        if (reqVO.getExpireTime() != null && reqVO.getExpireTime().length == 2) {
            wrapper.between(StudentDO::getExpireTime, reqVO.getExpireTime()[0], reqVO.getExpireTime()[1]);
        }
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(StudentDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        if (reqVO.getStudentBirthdate() != null && reqVO.getStudentBirthdate().length == 2) {
            wrapper.between(StudentDO::getStudentBirthdate, reqVO.getStudentBirthdate()[0], reqVO.getStudentBirthdate()[1]);
        }
        // 处理 u_status 条件 - 要考虑 wx_user 为 null 的情况
        if (reqVO.getUStatus() != null) {
            wrapper.and(w -> w
                    .isNull(WxUserDO::getId)  // wx_user 不存在的情况
                    .or()
                    .eq(WxUserDO::getUStatus, reqVO.getUStatus())  // wx_user 存在且 status 匹配
            );
        }

        // 处理 u_role 条件 - 要考虑 wx_user 为 null 的情况
        wrapper.and(w -> w
                .isNull(WxUserDO::getId)  // wx_user 不存在的情况
                .or()
                .eq(WxUserDO::getURole, 0)  // wx_user 存在且 role = 1
        );
        wrapper.orderByDesc(StudentDO::getId);

        // 执行连接查询并分页
        IPage<StudentUserDTO> resultPage = selectJoinPage(page, StudentUserDTO.class, wrapper);

        // 转换为框架的PageResult
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }

    default StudentUserDTO selectStudentUser(Long id) {
        return selectJoinOne(StudentUserDTO.class,
                new MPJLambdaWrapper<StudentDO>()
                        .selectAll(StudentDO.class)
                        .leftJoin(WxUserDO.class, WxUserDO::getId, StudentDO::getUserId)

                        // 关联表的列全部显式别名到 DTO 属性名（用“字符串别名”避免大小写问题）
                        .selectAs(WxUserDO::getId,           "uId")
                        .select(WxUserDO::getUOpenId)
                        .select(WxUserDO::getUName)
                        .select(WxUserDO::getUAvatar)
                        .select(WxUserDO::getUStatus)  // 关键：别名必须是 uStatus（小写u）

                        .eq(StudentDO::getId, id)
        );
    }

    default StudentUserDTO selectStudentUserByUserId(Long userId) {
        return selectJoinOne(StudentUserDTO.class,
                new MPJLambdaWrapper<StudentDO>()
                        .selectAll(StudentDO.class)
                        .leftJoin(WxUserDO.class, WxUserDO::getId, StudentDO::getUserId)

                        // 关联表的列全部显式别名到 DTO 属性名（用“字符串别名”避免大小写问题）
                        .selectAs(WxUserDO::getId,           "uId")
                        .select(WxUserDO::getUOpenId)
                        .select(WxUserDO::getUName)
                        .select(WxUserDO::getUAvatar)
                        .select(WxUserDO::getUStatus)  // 关键：别名必须是 uStatus（小写u）

                        .eq(StudentDO::getUserId, userId)
        );
    }

    default StudentUserDTO getStudentByPhoneNumber(String phoneNumber) {
        return selectJoinOne(StudentUserDTO.class,
                new MPJLambdaWrapper<StudentDO>()
                        .selectAll(StudentDO.class)
                        .leftJoin(WxUserDO.class, WxUserDO::getId, StudentDO::getUserId)

                        // 关联表的列全部显式别名到 DTO 属性名（用“字符串别名”避免大小写问题）
                        .selectAs(WxUserDO::getId,           "uId")
                        .select(WxUserDO::getUOpenId)
                        .select(WxUserDO::getUName)
                        .select(WxUserDO::getUAvatar)
                        .select(WxUserDO::getUStatus)  // 关键：别名必须是 uStatus（小写u）

                        .eq(StudentDO::getStudentPhoneNumber, phoneNumber)
        );
    }


    default List<StudentUserDTO> selectStudentList() {

        // 创建连接查询条件
        MPJLambdaWrapper<StudentDO> wrapper = new MPJLambdaWrapper<StudentDO>()
                .selectAll(StudentDO.class)  // 选择ProisDO所有字段
                .select(WxUserDO::getUName)
                .selectAs(WxUserDO::getId, StudentUserDTO::getUserId)
                .select(WxUserDO::getUOpenId)
                .select(WxUserDO::getUAvatar)
                .select(WxUserDO::getUStatus)// 选择SchoolDO的name字段
                .leftJoin(WxUserDO.class, on -> on
                        .eq(WxUserDO::getId, StudentDO::getUserId)
                );  // 连接条件

        // 处理 u_role 条件 - 要考虑 wx_user 为 null 的情况
        wrapper.and(w -> w
                .isNull(WxUserDO::getId)  // wx_user 不存在的情况
                .or()
                .eq(WxUserDO::getURole, 0)  // wx_user 存在且 role = 1
        );
        wrapper.orderByDesc(StudentDO::getId);

        return selectJoinList(StudentUserDTO.class, wrapper);
    }

}
